class OrderMaintain < ActiveRecord::Base
	belongs_to :user
	belongs_to :customer
	has_many :order_maintain_items, dependent: :destroy
	has_many :order_maintain_parts, dependent: :destroy

	enum status: %w(新建 已接车 服务中 已服务 已质检)
	enum payment_status: %w(未付款 已付款)

	after_create :gen_code

	def gen_code
	  code = rand(100000).to_s.rjust(6, "0")
  	  update_attributes(code: code)
	end

	# def update_service_status
 #      order_maintain_item = self.order_maintain_items.find_by_status(0..1)   
 #      if order_maintain_item.nil?
 #      	self.update(status: 3)
 #      end
	# end

	def update_receive_status
	  add_item = 0
	  add_part = 0
      order_maintain_items.each do |i|
        add_item = add_item + i.maintain_item.amount
      end
      order_maintain_parts.each do |p|
        add_part = add_part + p.sub_total
      end
      update(total: add_item + add_part, status: 1, cost: add_part)
	end


end
